import json

provinces = json.load(open('provinces.json', 'r', encoding='utf-8'))
cities = json.load(open('cities.json', 'r', encoding='utf-8'))
areas = json.load(open('areas.json', 'r', encoding='utf-8'))
streets = json.load(open('streets.json', 'r', encoding='utf-8'))

result = []

# 遍历省份
for province in provinces:
    temp = {}  # 临时变量
    # 添加省信息
    print(province['name'] + '...')
    temp['value'] = province['name']
    temp['label'] = province['name']
    temp['code'] = province['code']
    temp['children'] = []
    # 添加市信息
    for city in cities:
        if city['provinceCode'] == temp['code']:
            temp2 = {}
            temp2['value'] = city['name']
            temp2['label'] = city['name']
            temp2['code'] = city['code']
            temp2['children'] = []
            # 添加区信息
            for area in areas:
                if area['cityCode'] == temp2['code']:
                    temp3 = {}
                    temp3['value'] = area['name']
                    temp3['label'] = area['name']
                    temp2['children'].append(temp3)
            temp2.pop('code')
            temp['children'].append(temp2)
    temp.pop('code')
    result.append(temp)
with open('中国行政划分.json', 'w', encoding='utf-8') as f:
    f.write(str(result))
